package servlet;

import dao.UserDao;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author konley
 * @date 2020-06-17 19:52
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg = null;
        String url;
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        User user = new UserDao().getUserByName(name);
        if(user==null){ //查不到
            System.out.println("找不到");
            msg = "找不到该用户，请检查用户名！";
            url = "/index.jsp";
        }else if(!password.equals(user.getPassword())){ //密码错误
            System.out.println("密码错误");
            msg = "密码错误，请重新输入！";
            url = "/index.jsp";
        }else{  //登录成功
            System.out.println("全部正确");
            url = "/mistore.jsp";
        }
        request.setAttribute("msg",msg);
        request.getSession(true).setAttribute("user",user);
        request.getRequestDispatcher(url).forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
